﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace ServerCrafter.ClassLibrary.Tools
{
    internal static class BinarySerializer
    {
        public static void Serialize(string fileToOutputTo, Object objectToSerialize)
        {
            if (File.Exists(fileToOutputTo)) File.Delete(fileToOutputTo);
            var fileStream = new FileStream(fileToOutputTo, FileMode.CreateNew);
            var binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(fileStream, objectToSerialize);
            fileStream.Close();
            fileStream.Dispose();
        }

        public static void Deserialize(string fileToDeserializeFrom, out string[,] stringToDeserializeTo)
        {
            var fileStream = new FileStream(fileToDeserializeFrom, FileMode.Open);
            var binaryFormatter = new BinaryFormatter();
            stringToDeserializeTo = (string[,])binaryFormatter.Deserialize(fileStream);
            fileStream.Close();
            fileStream.Dispose();
        }
    }
}